﻿using System;
using System.Web;
using System.Reflection;
using System.Collections.Generic;
using Newtonsoft.Json;
using Intorise.Library.Common;
using System.Linq;
using System.Linq.Expressions;

public class StoreHandler : CommonHandler
{
    public void SubmitData()
    {
        var data = context.Request["data"];
        if (string.IsNullOrEmpty(data))
        {
            WriteResultJson(false, "提交的数据错误！"); return;
        }

        List<StoreInfo> datas = null;
        try
        {
            datas = JsonConvert.DeserializeObject<List<StoreInfo>>(data);
        }
        catch (Exception ex)
        {
            WriteResultJson(false, ex.Message); return;
        }

        lock (Manager.Stores)
        {
            datas.ForEach(x =>
            {
                var item = Manager.Stores.FirstOrDefault(f => f.Account == x.Account && f.ServerID == x.ServerID);
                if (item == null)
                {
                    item = x;
                    Manager.Stores.Add(x);
                }
                else
                {
                    item.ID = x.ID;
                    if (item.ID != "0")
                    {
                        item.Info = x.Info;
                    }
                    item.Diamond = x.Diamond;
                    item.Character = x.Character;
                }
            });
        }
        WriteResultJson(true);
    }

    public void LoadData()
    {
        int pageIndex = 0;
        int pageSize = 0;
        string sortField = "";
        string sortOrder = "";
        int.TryParse(context.Request["pageIndex"], out pageIndex);
        int.TryParse(context.Request["pageSize"], out pageSize);
        if (pageIndex < 0)
        {
            pageIndex = 0;
        }

        if (pageSize <= 0)
        {
            pageSize = 100;
        }

        sortField = context.Request["sortField"];
        sortOrder = context.Request["sortOrder"];
        if (string.IsNullOrEmpty(sortOrder))
        {
            sortField = "asc";
        }
        if (string.IsNullOrEmpty(sortField))
        {
            sortField = "account";
        }
        sortField = sortField.ToLower();
        Expression<Func<StoreInfo, object>> orderFunc = null;

        switch (sortField)
        {
            case "account":
                orderFunc = x => x.Account;
                break;
            case "serverid":
                orderFunc = x => x.ServerID;
                break;
            case "character":
                orderFunc = x => x.Character;
                break;
            case "diamond":
                orderFunc = x => x.Diamond;
                break;
            default:
                orderFunc = x => x.Info;
                break;
        }

        IQueryable<StoreInfo> query = Manager.Stores.AsQueryable();
        if (sortOrder == "asc")
        {
            query = query.OrderBy(orderFunc);
        }
        else
        {
            query = query.OrderByDescending(orderFunc);
        }

        string key = context.Request["key"];
        if (!string.IsNullOrEmpty(key))
        {
            query = query.Where(x => x.Account.Contains(key) || x.ServerID.Contains(key) || x.Info.Contains(key));
        }

        int total = query.Count();
        query = query.Skip(pageIndex * pageSize).Take(pageSize);
        lock (Manager.Stores)
        {
            WriteResultJson(new { total = total, data = query.ToList() });
        }
    }

    public void Delete()
    {
        List<Guid> ids = null;
        try
        {
            ids = JsonConvert.DeserializeObject<List<Guid>>(context.Request["data"]);
        }
        catch (Exception ex)
        {
            WriteSubmitInvalidaDataJson(); return;
        }

        lock (Manager.Stores)
        {
            Manager.Stores.RemoveAll(new Predicate<StoreInfo>((x) => { return ids.Contains(x.Index); }));
        }
        WriteResultJson(true);
    }
}